<html>
<head>
<title>爽团</title>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
//every 1 minute
//check for expired deals
//if deal expired, get new data
//set badge text = deals that started after last check

var x_json = null;

$(window).ready(function() {
        chrome.browserAction.setBadgeBackgroundColor({color:[75, 193, 222, 255]});
        checkForUpdate();
        var update = setInterval(checkForUpdate, 60000);
        var refresh_json = setInterval(resetJSON, 3600000);
});

function checkForUpdate() {
        //console.log(x_json);
        /* user has not set city yet */
        if (localStorage['city_xml'] == null) {
                return;
        }
        
        /* json failure or first load */
        if (x_json == null) {
                $.ajax({url:'http://www.groupon.com/api/v1/'+localStorage['city_xml']+'/deals?format=json', beforeSend:addAPI, dataType:'json', error:errorData, success:parseData });
                return;
        }
        
        /* expired deals */
        var expired = false;
        var now = new Date();
        for (var x in x_json.deals) {
                var expires = makeDate(x_json.deals[x].end_date);
                if ((expires.getTime() - now.getTime()) < 0) {
                        expired = true;
                }
        }       
        if (expired) {
                $.ajax({url:'http://www.groupon.com/api/v1/'+localStorage['city_xml']+'/deals?format=json', beforeSend:addAPI, dataType:'json', error:errorData, success:parseData });
        }
}

function addAPI(xhr) {
        xhr.setRequestHeader('X-GrouponToken', 'e97b22bbbffc1b83d8d8f6fec6a55e3f15dcb8ab');
}

function errorData() {
        /* put something here */
}

function makeDate(timestamp) {
        //console.log(timestamp);
        var end_date = timestamp;
        var yyyy = end_date.substring(0, 4);
        var mo = parseInt(end_date.substring(5, 7)) - 1;
        var dd = end_date.substring(8, 10);
        var hh = end_date.substring(11, 13);
        var mi = end_date.substring(14, 16);
        var ss = end_date.substring(17, 19);
        var end_date_obj = new Date(Date.UTC(yyyy, mo, dd, hh, mi, ss));
        return end_date_obj;
}

function parseData(json) {
        if (json != null) {
                var new_items = 0;
                x_json = json;
                if (localStorage['last_checked'] == null || isNaN(localStorage['last_checked'])) {
                        localStorage['last_checked'] = 0;
                }
                for (var x in x_json.deals) {
                        var started = makeDate(x_json.deals[x].start_date);
                        if ((localStorage['last_checked'] - started.getTime()) < 0) {
                                new_items++;
                        }
                }
                if (new_items > 0) {
                        chrome.browserAction.setBadgeText({text:new_items.toString()});
                } else {
                        chrome.browserAction.setBadgeText({text:''});   
                }
        }
}

function getJSON() {
        if (x_json == null) {
                checkForUpdate()
        }
        return x_json;
}

function resetJSON() {
        x_json = null;
        checkForUpdate();
}
</script>
</head>
<body>
</body>
</html>